package nix.mybatis;



import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

import nix.mybatis.pojo.User;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

public class MyBatisTest1 {

	@Test
	public void fun1() throws IOException {
		
		// 加载核心配置文件
		InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
		// 创建SqlSessionFactory
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
		// 创建SqlSession
		SqlSession sqlSession = sqlSessionFactory.openSession();
		// 执行sql
		User user = sqlSession.selectOne("user.findById", 10);
		System.out.println(user);
	}
	
	@Test
	public void fun2() throws IOException {
		
		// 加载核心配置文件
		InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
		// 创建SqlSessionFactory
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
		// 创建SqlSession
		SqlSession sqlSession = sqlSessionFactory.openSession();
		// 执行sql
		List<User> list = sqlSession.selectList("user.findListByUsername", "五");
		System.out.println(list);
	}
	
	@Test
	public void fun3() throws IOException {
		
		// 加载核心配置文件
		InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
		// 创建SqlSessionFactory
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
		// 创建SqlSession
		SqlSession sqlSession = sqlSessionFactory.openSession();
		// 执行sql
		User user = new User();
		user.setUsername("张小强");
		int i = sqlSession.insert("user.add", user);
		// 提交事务
		sqlSession.commit();
		System.out.println(i);
	}
	
	@Test
	public void fun4() throws IOException {
		
		// 加载核心配置文件
		InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
		// 创建SqlSessionFactory
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
		// 创建SqlSession
		SqlSession sqlSession = sqlSessionFactory.openSession();
		// 执行sql
		User user = new User();
		user.setUsername("张大强");
		sqlSession.insert("user.addAndGetId", user);
		// 提交事务
		sqlSession.commit();
		System.out.println(user.getId());
	}
	
	@Test
	public void fun5() throws IOException {
		
		// 加载核心配置文件
		InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
		// 创建SqlSessionFactory
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
		// 创建SqlSession
		SqlSession sqlSession = sqlSessionFactory.openSession();
		// 执行sql
		User user = sqlSession.selectOne("findById", 28);
		user.setBirthday(new Date());
		int i = sqlSession.update("user.update", user);	
		sqlSession.commit();
		System.out.println(i);
	}
	
	@Test
	public void fun6() throws IOException {
		
		// 加载核心配置文件
		InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
		// 创建SqlSessionFactory
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
		// 创建SqlSession
		SqlSession sqlSession = sqlSessionFactory.openSession();
		// 执行sql
		int i = sqlSession.delete("user.delete", 28);
		sqlSession.commit();
		System.out.println(i);
	}
}
